<template>
  <div>
    <h2>客户管理</h2>
<!--    <el-card class="box-card">-->
<!--      <div slot="header" class="clearfix">-->
<!--        <span class="het">客户管理</span>-->
<!--        <el-button style="margin-left: 70%" type="success" size="small" icon="el-icon-plus" @click="openDialog">新增</el-button>-->
<!--        <el-button type="danger" size="small" icon="el-icon-delete" @click="delBatch">批量删除</el-button>-->
<!--      </div>-->
<!--    </el-card>-->

    <el-form
      ref="searchForm"
      :inline="true"
      :model="keyword"
      class="demo-form-inline"
      style="padding-top: 10px;margin-left: 35px"
    >
      <el-form-item label="客户名称:" prop="cliName" style="margin-left: 35px">
        <el-input style="width: 204px" v-model="keyword.cliName" placeholder="请输入客户名称" />
        <el-button style="margin-left: 10px" type="primary" size="small" icon="el-icon-search" @click="search">查询</el-button>
        <el-button type="warning" size="small" icon="el-icon-refresh-right" @click="resetSearchForm('searchForm')">重置</el-button>
      </el-form-item>

      <!-- 下拉框 -->
      <el-form
        ref="form"
        :inline="true"
        :model="records"
        class="demo-form-inline"
        style="padding-top: 10px;margin-left: 35px">

        <el-form-item label="意向等级:">   <!--   style="margin-left: 15px"     -->
          <el-select v-model="keyword.yiLv" placeholder="请选择" @change="search">
            <el-option v-for="roleType in List3" :label="roleType" :value="roleType" />
          </el-select>
        </el-form-item>

        <el-form-item label="客户等级:">              <!--style="margin-left: 15px"  v-if="show==true"-->
          <el-select v-model="keyword.cliLv" placeholder="请选择" @change="search">
            <el-option v-for="roleType in List3" :label="roleType" :value="roleType" />
          </el-select>
        </el-form-item>

        <el-form-item label="跟进状态:">              <!--style="margin-left: 15px"  v-if="show==true"-->
          <el-select v-model="keyword.status" placeholder="请选择" @change="search">
            <el-option v-for="roleType in List3" :label="roleType" :value="roleType" />
          </el-select>
        </el-form-item>
      </el-form>

      <!-- 新增/详情 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible" width="44%">
        <el-form :model="contract" :rules="contractRules" ref="contractRulesForm">
          <el-form-item label="id" prop="id">
            <el-input :disabled="true" v-model="contract.id" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="客户名称" prop="cliName">
            <el-input v-model="contract.cliName" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <!--        <el-form-item label="签订时间" prop="createTime">-->
          <!--          <el-input v-model="contract.createTime" autocomplete="off" style="width: 300px"></el-input>-->
          <!--        </el-form-item>-->
          <el-form-item label="意向等级" prop="yiLv">
            <el-input v-model="contract.yiLv" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="客户等级" prop="cliLv">
            <el-input v-model="contract.cliLv" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="跟进状态" prop="status">
            <el-input v-model="contract.status" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="跟进人" prop="cliPeople">
            <el-input v-model="contract.cliPeople" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
        </el-form>
        <!--      操作选项-->
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <!--   点确定触发函数  进行校验 -->
          <el-button type="primary" @click="addContract('contractRulesForm')">保 存</el-button>
        </div>
      </el-dialog>

      <!-- 表格数据区域-->
      <el-table :data="clientList" style="width: 100%;margin-bottom: 20px;">
        <!--      <el-table-column prop="id" align="center" label="租户名称" />-->
        <el-table-column prop="cliName" align="center" label="客户名称" />
        <el-table-column prop="yiLv" align="center" label="意向等级" />
        <el-table-column prop="cliLv" align="center" label="客户等级" />
        <el-table-column prop="status" align="center" label="跟进状态" />
        <el-table-column prop="cliPeople" align="center" label="跟进人" />
        <el-table-column prop="createTime" align="center" label="新增时间" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleFind(scope.row)"
            >详情
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>

    <!--完整功能分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 7, 10,15,20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  </div>
</template>

<script>

import { getList, delList, addList } from '@/api/client'
export default {
  name: 'Client',
  data() {
    return {
      title: '',
      keyword: { // 添加查询关键词对象
        id: '',
        cliName: null,
        yiLv: null,
        cliLv: null,
        status: null
      },
      dialogFormVisible: false,
      loading: false,
      contract: {
        id: '',
        cliName: '',
        yiLv: '',
        cliLv: '',
        status: '',
        cliPeople: ''
      },
      clientList: [],
      currentPage: 1,
      pageNum: 1,
      pageSize: 7,
      total: 0,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getList().then(res => {
        this.clientList = res.data
      })
    },


    // fetchData() {
    //   const params = {
    //     pageNum: this.pageNum,
    //     pageSize: this.pageSize,
    //     id: this.keyword.id,
    //     cliName: this.keyword.cliName
    //   }
    //   getList(params).then(res => {
    //     const pageInfo = res.data
    //     this.loading = false
    //     this.clientList = pageInfo.list
    //     this.total = pageInfo.total
    //   })
    // },
    // 搜索
    search() {
      this.fetchData()
    },
    // 重置
    resetSearchForm(formName) {
      this.$refs[formName].resetFields()
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchData()
    },


    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除客户名称为' + row.cliName + '的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          id: row.id
        }
        delList(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 详情
    handleFind(row) {
      this.dialogFormVisible = true
      this.contract = row
      this.title = '确认基本信息'
    },
    addContract(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === '确认基本信息') {
            // editList(this.contract).then(res => {
            this.dialogFormVisible = false
            this.fetchData()
            // })
          } else if (this.title === '合同基本信息') {
            addList(this.contract).then(res => {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.dialogFormVisible = false
              this.fetchData()
            })
          }
        } else {
          return false
        }
        this.dialogFormVisible = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
